import { Component } from '@angular/core';
import { PopoverController } from '@ionic/angular';
import { PopoverComponent } from './component/popover/popover.component';
import { NavController } from '@ionic/angular';
import { Storage } from '@ionic/storage';
import { StorageService } from '../../services/storage/storage.service';
import { GetAppPortalService } from '../../services/get-app-portal.service';
import { first } from 'rxjs/operators';
import { LoadingController } from '@ionic/angular';
import { ActivatedRoute, Params } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { LogoutService } from '../../services/logout/logout.service';
@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {
  public name: string;
  public type: string;
  public  portalTile:string;
  public portalKey:string;
  public data = [
   
  ];
  public portalInfo: any;
  public loading: any

  constructor(
    public popoverController: PopoverController,
    public Nav: NavController,
    private storage: Storage,
    public storageService: StorageService,
    public geapp: GetAppPortalService,
    public logoutService:LogoutService,
    public loadingController: LoadingController,
    public activeRoute: ActivatedRoute,
    public translate:TranslateService,
  ) {
    this.activeRoute.queryParams.subscribe(res => {
      console.log(res);
      this.portalTile=res.title
      if(res.key){
        console.log("laile")
        
       // this.data.unshift({"name":res.key})
        this.data=this.getDataBykey(res.title,"Title")
      }
      
    });
    this.show()
    this.storage.get("loginDetails").then(data => {
    //  alert(JSON.stringify(data))
      console.log(data)
      this.geapp.getPortalInfo(data).pipe(first())
        .subscribe(data => {
          console.log(data)
          this.portalInfo = data
          this.portalTile=data.selectedPortal
          console.log("来了")
          console.log(this.portalTile)
          this.data=this.getDataBykey(this.portalTile,"Title")
          this.hide()
        })

    })
  }

  ngOnInit(){
   // this.data=this.getDataBykey(this.portalTile)
  }
  async presentPopover(ev: any) {
    const popover = await this.popoverController.create({
      component: PopoverComponent,
      event: ev,
      componentProps: { type: "setup",portalTile:this.portalTile },
      translucent: true
    });
    return await popover.present();
  }
  async presentPopoverPortal(ev: any) {
    const popover = await this.popoverController.create({
      component: PopoverComponent,
      event: ev,
      componentProps: { type: "portal", portal: this.portalInfo },
      translucent: true
    });
    return await popover.present();
  }
  logout() {
    //portalTile http://oa.jf81.com/sfv3/appmgt.nsf/xp_ws.xsp/Logout?&email=zding@jf81.com&languageCode=zh&portalGroup=app.integrum Group A
    console.log(this.portalTile)
    let lan=this.translate.getDefaultLang();
    console.log(this.logoutService)
    this.storage.get("loginDetails").then(data => {
      this.logoutService.setLogout(data.username,data.password,data.email,lan,this.portalTile).pipe(first())
      .subscribe(res => {
        console.log(res)
        if(res.status){
          console.log('退出登录');
          this.Nav.navigateRoot('loginpass');
          localStorage.setItem('hasLogged', "false");
        }
      })
  })
   
  
    
  }
  getInfo() {

  }
  async  show() {
    this.loading = await this.loadingController.create({
      message: 'loading....',
      duration: 2000
    });
    await this.loading.present();
  }
  async hide() {
    if (this.loading) {
      await this.loading.dismiss();
    }
  }

   getDataBykey(key:string,objkey:string):any{
  //   console.log("查找关键信息===")
      let res:any;
     this.portalInfo.items.forEach(element => {
         console.log(objkey)
         console.log(element[objkey])
         console.log(element.Title)
         console.log(key)
         if(element[objkey].trim()==key.trim()&&element[objkey]!=""){
           console.log("找到了")
           console.log(element.allportal)
           res= this.getNoBlankData(element.allportal)
         }
     });
     return res
  }
  getNoBlankData(data:any):any{
          let arr=[];
          let index=0
          data.forEach(function (obj) {
              if(obj.LinkTitle!=""){
               // obj.index=index
               obj.SFMImage="sfv3/"+obj.SFMImage
                arr.push(obj)
              //  index=index+1
              }
          })
          return arr;
  }



}
